﻿using Dapper.Extensions;
using System;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;
using YouWei.Utility;
using YouWei.Utility.Logging;
using System.Linq;
using System.Collections.Generic;
using YouWei.Infrastructure.Repository;

namespace YouWei.Op.Controllers
{
    public class CarLockController : BaseController
    {
        private CarLockService _service = new CarLockService();
        /// <summary>
        /// 管理列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CarLockIndex()
        {
            return View();
        }

        /// <summary>
        /// 列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ContentResult CarLockList(CarLockSearchModel model)
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            Page<CarLockDto> page = _service.Pages(pageIndex, pageSize, model);
            return ReturnResult(page);
        }


        /// <summary>
        /// 编辑视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CarLockEidt()
        {
            var id = GetParam<int>("id");
            CarLock entity;
            if (id > 0)
                entity = _service.Single(id);
            else
                entity = new CarLock();
            ViewBag.UserName = "";
            ViewBag.UserMobile = "";
            if (entity.UserId > 0)
            {
                var user = new UserInfoService().Single(entity.UserId);
                if (user != null)
                {
                    ViewBag.UserName = user.UserName;
                    ViewBag.UserMobile = user.Mobile;
                }
            }
            return View(entity);
        }
        /// <summary>
        /// 添加优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CarLockSave(CarLock model)
        {
            ResultData<int> rt = ResultData<int>.Error("");
            if (string.IsNullOrWhiteSpace(model.Address))
            {
                rt.message = "缺少必要的参数Address";
                return ReturnResult(rt);
            }
            model.ID = GetParam<int>("id");
            //edit
            if (model.ID > 0)
            {
                var entity = _service.Single(model.ID);
                if (entity == null)
                {
                    rt.message = "id不正确，信息不存在";
                    return ReturnResult(rt);
                }
                //不需要更新的字段从entity带过来
                entity.LockNum = model.LockNum;
                entity.ProvinceID = model.ProvinceID;
                entity.CityID = model.CityID;
                entity.DistrictID = model.DistrictID;
                entity.Address = model.Address;
                entity.Parking = model.Parking;
                entity.UpdateDate = DateTime.Now;
                var re = _service.Update(entity);
                if (!re)
                {
                    rt.message = "更新失败";
                    return ReturnResult(rt);
                }
                else
                {
                    rt.status = 0;
                    rt.message = "更新成功";
                }
            }
            //add
            else
            {
                model.LockStatus = 0;
                model.IsActive = 1;
                model.UserId = 0;//新建地锁默认用户为0
                model.CreateDate = DateTime.Now;
                model.Channel = "";
                model.LockKey = "";
                _service.Insert(model);
                rt.status = 0;
                rt.message = "添加成功";
            }
            return ReturnResult(rt);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CarLockDelete()
        {
            ResultData<int> rt = ResultData<int>.Error("");
            var id = GetParam<int>("id");
            if (id <= 0)
            {
                rt.message = "缺少必要的参数";
                return ReturnResult(rt);
            }
            var entity = _service.Single(id);
            if (entity != null)
            {
                entity.IsActive = 0;
                entity.UpdateDate = DateTime.Now;
                var res = _service.Update(entity);
                if (res)
                {
                    rt.status = 0;
                    rt.message = "删除成功";
                }
                else
                {
                    rt.status = -1;
                    rt.message = "删除失败";
                }

            }
            else
            {
                rt.message = "id不正确，内容不存在";
            }
            return ReturnResult(rt);
        }
    }
}